package com.wwwarehouse.carddesk.fragment.teamfragment.managerfragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.carddesk.bean.teambean.TeamManagerUserInfoBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/13 16:52
 * @描述 ${TODO}
 */

public class ManagerLookWatcherFragment extends BaseFragment implements View.OnClickListener{

    private View                        mView;
    private CircleImageView             mCivHead;
    private TextView                    mTvName;
    private TextView                    mTvRole;
    private TextView                    mTvCompanyName;
    private RelativeLayout.LayoutParams mParams;
    private LinearLayout                mLlNoData;
    private StateButton                 mBtConnect;
    private StateButton                 mBtBackTeam;
    private CircleImageView             mCivCreater;
    private TextView                    mTvCraeter;
    private CircleImageView             mCivManager;
    private TextView                    mTvManager;
    private CircleImageView             mCivWatcher;
    private TextView                    mTvWatcher;
    private RelativeLayout              mRlHaveData;
    private CardDeskGroupGetGroupBean.ListBean mBean;
    private StateLayout mStateLayout;
    private Map<String, Object> mMap;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_manager_look_watcher, container, false);
        initView();
        return mView;
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_layout);
        mCivHead = findView(mView, R.id.civ_head);
        mTvName = findView(mView, R.id.tv_name);
        mTvRole = findView(mView, R.id.tv_role);
        mTvCompanyName = findView(mView, R.id.tv_company_name);
        mCivCreater = findView(mView, R.id.civ_creater);
        mTvCraeter = findView(mView, R.id.tv_creater);
        mCivManager = findView(mView, R.id.civ_manager);
        mTvManager = findView(mView, R.id.tv_manager);
        mCivWatcher = findView(mView, R.id.civ_watcher);
        mTvWatcher = findView(mView, R.id.tv_watcher);
        mRlHaveData = findView(mView, R.id.rl_have_data);
        mLlNoData = findView(mView, R.id.ll_no_data);
        mBtConnect = findView(mView, R.id.bt_connect);
        mBtBackTeam = findView(mView, R.id.bt_back_team);

        mCivCreater.setOnClickListener(this);
        mCivManager.setOnClickListener(this);
        mCivWatcher.setOnClickListener(this);
        mBtConnect.setOnClickListener(this);
        mBtBackTeam.setOnClickListener(this);

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ManagerLookWatcherFragment) {
            mActivity.setTitle(R.string.teamdesk_member);
        }
    }

    @Override
    public void requestDatas() {
        if (getArguments() != null && getArguments().getParcelable("bean") != null) {
            Bundle bundle = getArguments();
            mBean = (CardDeskGroupGetGroupBean.ListBean) bundle.getParcelable("bean");
            BaseApplication.getApplicationInstance().displayImg(mBean.getFaceUrl(), mCivHead);
            mTvName.setText(mBean.getPersonName());
            mTvRole.setText(mBean.getCardManagerType());
            mTvCompanyName.setText(String.format("    %s    ", mBean.getBuName()));

            mMap = new HashMap<>();
            mMap.put("buId",mBean.getBuId() );
            getThreeConnect();

        }
    }

    private void getThreeConnect() {
        //三角恋接口
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(CardDeskConstant.GET_MANAGER_USER_INFO, mMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener=new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            try {
                switch (what) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
//                            mRlHaveData.setVisibility(View.VISIBLE);
//                            mLlNoData.setVisibility(View.INVISIBLE);
                            TeamManagerUserInfoBean teamManagerUserInfoBean = JSON.parseObject(commonClass.getData().toString(), TeamManagerUserInfoBean.class);
                            TeamManagerUserInfoBean.CreatorBean creator = teamManagerUserInfoBean.getCreator();//创造者
                            TeamManagerUserInfoBean.RegulatorBean regulator = teamManagerUserInfoBean.getRegulator();//管理者
                            TeamManagerUserInfoBean.SupervisorBean supervisor = teamManagerUserInfoBean.getSupervisor();//监控者
                            BaseApplication.getApplicationInstance().displayImg(creator.getFaceUrl(), mCivCreater);
                            BaseApplication.getApplicationInstance().displayImg(regulator.getFaceUrl(), mCivManager);
                            mTvManager.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_group_adapter_managers),regulator.getName()));
                            BaseApplication.getApplicationInstance().displayImg(supervisor.getFaceUrl(), mCivWatcher);
                            mTvWatcher.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_group_adapter_supervisors),supervisor.getName()));
                        } else {
//                            mRlHaveData.setVisibility(View.INVISIBLE);
//                            mLlNoData.setVisibility(View.VISIBLE);
                        }
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String result, int what) {
//            mRlHaveData.setVisibility(View.INVISIBLE);
//            mLlNoData.setVisibility(View.VISIBLE);
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getThreeConnect();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bt_connect) {
            if (mBean.getMobile() != null) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mBean.getMobile()));
                startActivity(intent);
            }
        } else if (id == R.id.bt_back_team) {
            popFragment();
        }
    }
}
